<?php

namespace app\api\controller;

use think\Db;
// use app\common\controller\Api;
use think\Controller;
use app\api\Controller\QrcodeServer;

class Group extends Controller
{
	/** 
	* 拼团列表页(分类)
	* @access public 
	* @param $all 所有拼团商品二级分类id集合(数组)
	* @param $two_type 所有拼团商品二级分类id集合(子串)
	* @param $one_type 所有拼团商品一级分类id集合(数组)
	* @param $type 所有拼团商品一级分类信息集合(数组)
	* @return strig JSON数据
	*/
	public function all_type()
	{
		$time = time();
		$all = Db::name("group g")
				->join("group_goods gg", 'g.id=gg.group_id')
				->join("shopro_goods sg", 'gg.goods_id=sg.id')
				// ->where("g.status", '1')
				// ->where("sg.status", 'up')
				// ->where("g.start_time", "<", $time)
				// ->where("g.end_time", ">", $time)
				->column("sg.category_ids");
		$two_type = implode(",", $all);
		// $two_type_array = explode(",", $two_type);
		$one_type = Db::name("shopro_category")
				->where('find_in_set(id,:two_type)',['two_type'=>$two_type])
				->column("pid");
		$type = Db::name("shopro_category")
				->whereIn("id", $one_type)
				->field("id,name")
				->select();
		array_unshift($type, ['id'=>0, 'name'=>'全部']);
		return success_result($type);
	}

	/** 
	* 拼团列表页(商品列表)
	* @access public 
	* @param $type_id 接受前台的一级分类id
	* @param $goods 拼团商品信息
	* @return strig JSON数据
	*/
	public function type_list()
	{
		$type_id = input("type_id");
		if($type_id != 0){
			$two_type = Db::name("shopro_category")->where("pid", $type_id)->column('id');
			$where['sg.category_ids'] = ['in', $two_type];
		}else{
			$where = [];
		}
		$time = time();

		$goods = Db::name("group_goods gg")
				->join("group g", 'g.id=gg.group_id')
				->join("shopro_goods sg", 'gg.goods_id=sg.id')
				->join("group_goods_sku ggs", 'ggs.gg_id=gg.id')
				->join('group_astrict gat', 'ggs.id = gat.ggs_id')
				->where($where)
				->order("gat.price")
				->group('gg.id')
				// ->where('find_in_set(sg.category_ids,:two_type)',['two_type'=>$two_type])
				// ->where("g.status", 1)
				// ->where("sg.status", 'up')
				// ->where("g.start_time", "<", $time)
				// ->where("g.end_time", ">", $time)
				->field("gg.id, sg.title, sg.image, sg.original_price, gat.person_num, gat.price")
				->select();
		return success_result($goods);
	}

	/** 
	* 拼团详情页(商品详情)
	* @access public 
	* @param $id 接受前台的拼团商品group_goods_id
	* @param $goods_info 拼团商品规格数据
	* @param $sku_price_array 拼团商品规格价格数组
	* @return strig JSON数据
	*/
	public function detail()
	{
		$id = input('group_goods_id');
		$goods_info = Db::name("group_goods gg")
					->where("gg.id", $id)
					->join("shopro_goods sg", 'gg.goods_id = sg.id')
					->join("group g", "gg.group_id=g.id")
					->join("group_goods_sku ggs", "ggs.gg_id = gg.id")
					->join("group_astrict gat", "gat.ggs_id = ggs.id", 'left')
					->join("shopro_goods_sku_price sgkp", 'sgkp.id = ggs.sku_price_id')
					->order("gat.price")
					->limit(1)
					// ->join("shopro_goods_sku sgs", "find_in_set(sgs.id, ggs.sku_ids)") sgs.name
					->field("sg.id as id, sg.original_price, sg.title, sg.subtitle, sg.images, gat.price, gat.person_num, g.end_time, ggs.sku_price_id, ggs.id as sku_id, sgkp.goods_sku_ids, sgkp.goods_sku_text")
					->find();
		$all_type = Db::name("shopro_goods_sku")
						->where("goods_id", $id)
						->where("pid", 0)
						->field("id, name")
						->select();
		foreach($all_type as $key=>$value){
			$all_type[$key]['child'] = Db::name("shopro_goods_sku")
										->where("pid", $value['id'])
										->field('name, id')
										->fieldRaw("if(find_in_set(id, '".$goods_info['goods_sku_ids']."'), true, false) as checked")
										->select();
		}

		$sku_price_array = Db::name("group_goods gg")
						->where("gg.id", $id)
						->join("shopro_goods sg", 'gg.goods_id = sg.id')
						->join("group g", "gg.group_id=g.id")
						->join("group_goods_sku ggs", "ggs.gg_id = gg.id")
						->join("group_astrict gat", "gat.ggs_id = ggs.id", 'left')
						->join("shopro_goods_sku_price sgkp", 'sgkp.id = ggs.sku_price_id')
						->field("sgkp.goods_sku_ids, sgkp.price, ggs.id as pk_id, gat.*")
						->select();
		$data = [
			'gooods_info' => $goods_info,
			'all_type' => $all_type,
			'sku_price_array' => $sku_price_array
		];
		return success_result($data);
		
	}

	/** 
	* 拼团一键开团页面(商品信息)
	* @access public 
	* @param $sku_id 接受前台的拼团商品规格sku_id
	* @param $uid 接受前台的用户id
	* @param $goods_info 拼团商品信息
	* @param $examine_time 当前时间(检测活动是否结束)
	* @param $examine_grade 会员等级(检测用户等级是否足够)
	* @param $examine_buy 是否购买(检测用户是否购买过此拼团商品)
	* @param $result 所有该团成员信息
	* @param $lack_user 满员相差人数
	* @return strig JSON数据
	*/
	public function group_buy()
	{
		$sku_id = input("sku_id");
		//此处先模拟接受用户id 需要修改
		$uid = input("uid");
		if(empty($uid)){
			return error_result("请先登录");
		}
		$goods_info = Db::name("group_goods_sku ggs")
						->where("ggs.id", $sku_id)
						->join("group_goods gg", 'gg.id = ggs.gg_id')
						->join("shopro_goods sg", 'gg.goods_id = sg.id')
						->join("shopro_goods_sku_price sgkp", 'sgkp.id = ggs.sku_price_id')
						->join("group_astrict gat", "gat.ggs_id = ggs.id", 'left')
						->join("group g", "gg.group_id=g.id")

						->field("sg.image, sg.title, sg.original_price, sgkp.goods_sku_text, gat.*, g.end_time, ggs.gg_id")
						->find();
		$examine_time = time();
		if($examine_time > $goods_info['end_time']){
			return error_result("该活动已结束");
		}
		$examine_grade = Db::name("user")->where("id", $uid)->value("level");
		if($examine_grade < $goods_info['valid_grade']){
			return error_result("抱歉,您的等级不够");
		}
		$examine_buy = Db::name("group_record")
						->where("uid", $uid)
						->where("group_goods_id", $goods_info['gg_id'])
						->value("belong_group");
		if(empty($examine_buy)){
			$incode = uniqid().rand(1111,9999);
			$data = [
				'uid' => $uid,
				'group_goods_id' => $goods_info['gg_id'],
				'ggs_id' => $sku_id,
				'is_head' => 1,
				'belong_group' => $incode,
				'create_time' => $examine_time
			];
			$insert_id = Db::name("group_record")->insertGetId($data);
			$result = Db::name("group_record gr")
						->where("gr.id", $insert_id)
						->join("user u", 'u.id = gr.uid')
						->field("u.avatar, gr.is_head")
						->select();
		}else{
			$incode = $examine_buy;
			$result = Db::name("group_record gr")
						->where("belong_group", $examine_buy)
						->join("user u", 'u.id = gr.uid')
						->field("u.avatar, gr.is_head")
						->select();
		}
		$lack_user = $goods_info['person_num'] - count($result);
		if($lack_user < 0){
			$lack_user = 0;
		}
		$data = [
			'goods_info' => $goods_info,
			'user_info' => $result,
			'lack_user' => $lack_user,
			'incode' => $incode
		];
		return success_result($data);
	}


	public function other_group_goods()
	{
		//查询其他有效的拼团商品
		$sku_id = input("sku_id");
		$goods_id = Db::name("group_goods_sku")->where("id", $sku_id)->value("gg_id");
		$time = time();
		$where = [
			'g.start_time' => ['<', $time],
			'g.end_time' => ['>', $time],
			'g.status' => ['=', '1'],
			'sg.status' => ['=', 'up'],
			// 'gg.id' => ['<>', $goods_id]
		];
		$group_goods = Db::name("group g")
						->where($where)
						->join("group_goods gg", 'g.id = gg.group_id')
						->join("shopro_goods sg", 'sg.id = gg.goods_id')
						->join("group_goods_sku ggs", 'ggs.gg_id=gg.id')
						->join('group_astrict gat', 'ggs.id = gat.ggs_id')
						->field("gg.id, sg.title, sg.image, sg.original_price, sg.subtitle, gat.person_num, gat.price")
						->order("gat.price")
						->limit(1)
						// ->fetchSql(true)
						->select();
		// var_dump($group_goods);die;
		return success_result($group_goods);
	}

	public function create_qrcode()
	{
		$url = input("url");
		$belong_group = input("incode");
		//此处先模拟接受用户id 需要修改
		$uid = input("uid");

        $config = [
            'title' => false,
            'logo' => true,
            'logo_url' => './微信图片_20200601161929.png',
            'logo_size' => 25,
            'generate'	=> 'writefile',
            'directory_name' => './static/qrcode/',
            'file_name' => $belong_group
        ];
        $qr_url = $url;
 
        $qr_code = new QrcodeServer($config);
        $qr_img = $qr_code->createServer($qr_url);

        var_dump($qr_img);
        // 二维码
        echo "<img src='http://1.com".$qr_img['data']['url']."'>";
        // //获取用户的头像和昵称
        // $user_info = Db::name("user")->where("id", $uid)->field('nickname', 'avatar')->find();
        // //获取商品的封面图
        // $goods_info = Db::name("group_record gr")
        // 				->where("belong_group", $belong_group)
        // 				->join("group_goods gg", "gg.id = gr.group_goods_id")
        // 				->join("shopro_goods sg", "sg.id = gg.goods_id")
        // 				->join("group_astrict gat", "")
        // 				->field("sg.image")
	}

	public function create_poster()
	{	

		header('Content-Type:image/png');
		$img = imagecreatetruecolor(285.5, 511);
		$white = imagecolorallocate($img, 255, 255, 255);
		imagefill($img, 100, 100, $white);

		imagepng($img);//以png格式输出到浏览器
		imagedestroy($img);//释放内存
		exit(0);		
	}
}
